﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Kirin.Objects;
using Kirin.Utility;

namespace KirinUI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                // load program settings from default directory
                Render.LoadSettings();

                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new Form1());

                // Save settings
                Render.Settings.Save();
            }
            catch (NullReferenceException)
            {
                // HACK
            }
            // uncomment this if you want easy debugging ;)
            //#if !DEBUG
            catch (Exception caught)
            {
                Exception e;
                string errorMessages;
                try
                {
                    // log the error
                    Log.Write(caught);
                }
                catch
                {
                    // ignore errors while trying to write the error to the log
                }
                finally
                {
                    e = caught;
                    errorMessages = "The following error(s) occurred:";
                    do
                    {
                        errorMessages += "\r\n" + e.Message;
                        e = e.InnerException;
                    }
                    while (e != null);
                    Abort(errorMessages);
                }
            }
            //#endif
        }

        #region Public methods

        /// <summary>
        /// Display an error message to the user, then shut down world wind.
        /// </summary>
        /// <param name="msg">The message (reason for shutdown) to display to the user.</param>
        public static void Abort(string errorMessages)
        {
            ErrorDisplay errorDialog = new ErrorDisplay();
            errorDialog.errorMessages(errorMessages);
            errorDialog.ShowDialog();
            Environment.Exit(0);
        }
        #endregion
    }
}